package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.model.SmkAppUser;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * upp2000 - 信息订阅查询
 *
 * @Author cwledit
 * @DATE 2017/6/13.
 */
@Service("upp2000")
public class Upp2000 extends BaseService implements IService{

    private String SERVER_NAME = "upp.service.msg.query";

    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws Exception {
//        Map paramMap =  request.getData();
//        String card_no = paramsStringFilter(paramMap.get("card_no"));
        SmkAppUser smkAppUser = AppUserKit.getCurrent(request);
        if (smkAppUser == null){
            return renderFAIL(MsgCode.Code_1007,response,header);
        }

        Map data = new HashMap();
        data.put("id_no",smkAppUser.getIdCard());
        data.put("id_type","01");
//        data.put("card_no",card_no);

        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME,request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            List<Map> list = (List) jsonObject.get("list");
            boolean result = false;
            for (Map map : list){
                if("3".equals(map.get("subscribe_type"))){
                    if ("1".equals(map.get("status"))){
                        result = true;
                    }
                }
            }
            return renderSUC(result,response,header);
        }else{
            return renderFAIL(MsgCode.Code_1009,"市民卡网关返回异常:"+jsonObject.getString("msg"),response,header);
        }
    }
}
